package com.rc.SplashScreen;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;

import com.rc.activity.DashBoardScreen;
import com.rc.activity.Login;
import com.rc.activity.R;
import com.rc.activity.dao.tbl_ItemDao;
import com.rc.activity.dao.tbl_ReceiptDao;
import com.rc.activity.dao.tbl_ReportDao;
import com.rc.activity.dao.tbl_TripDao;
import com.rc.activity.dao.tbl_TripItemsDao;
import com.rc.activity.dao.tbl_User;
import com.rc.activity.provider.ContentProviderApp;
import com.rc.db.DatabaseSupport;
import com.rc.util.Const;
import com.telpoo.frame.ui.BaseActivity;

public class SplashScreenActivity extends BaseActivity implements Runnable {

	public static void callIntent(Context ct) {
		Intent it = new Intent(ct, SplashScreenActivity.class);
		ct.startActivity(it);
	}

	public static void callIntentFinish(Activity ct, boolean isTrue) {
		Intent it = new Intent(ct, SplashScreenActivity.class);
		it.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		it.putExtra("IsTrue", isTrue);
		ct.startActivity(it);
	}

	private Thread mThread;
	private boolean active = true;
	private int mTime = 50;
	private tbl_User objUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getIntent() != null) {
			if (getIntent().getBooleanExtra("EXIT", false)) {
				finish();
				System.exit(0);
				return;
			}
		}

		if (getResources().getBoolean(R.bool.is_tablet)) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		if (!getResources().getBoolean(R.bool.is_tablet)) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.intro_page);

		// Check network
		// this.registerReceiver(Util.mConnReceiver, new IntentFilter(
		// ConnectivityManager.CONNECTIVITY_ACTION));
		String sqlReport = "DELETE FROM " + tbl_ReportDao.TABLENAME + " where "
				+ tbl_ReportDao.Properties.IsCreate.columnName + " = '2'";
		String sqlReceipt = "DELETE FROM " + tbl_ReceiptDao.TABLENAME
				+ " where " + tbl_ReceiptDao.Properties.IsCreate.columnName
				+ " = '2'";
		String sqlTrip = "DELETE FROM " + tbl_TripDao.TABLENAME + " where "
				+ tbl_TripDao.Properties.IsCreate.columnName + " = '2'";
		String sqlItem = "DELETE FROM " + tbl_ItemDao.TABLENAME + " where "
				+ tbl_ItemDao.Properties.IsCreate.columnName + " = '2'";
		String sqlTripItem = "DELETE FROM " + tbl_TripItemsDao.TABLENAME
				+ " where " + tbl_TripItemsDao.Properties.IsCreate.columnName
				+ " = '2'";
		ContentProviderApp.db.execSQL(sqlReport);
		ContentProviderApp.db.execSQL(sqlReceipt);
		ContentProviderApp.db.execSQL(sqlTrip);
		ContentProviderApp.db.execSQL(sqlItem);
		ContentProviderApp.db.execSQL(sqlTripItem);
	}

	@Override
	protected void onResume() {
		active = true;
		mTime = 50;
		super.onResume();
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if (mThread == null) {
			mThread = new Thread(this);
			mThread.start();
		}
		super.onWindowFocusChanged(hasFocus);
	}

	public void run() {
		try {
			while (active && mTime > 0) {
				mTime -= 10;
				Thread.sleep(300);
			}

		} catch (Exception e) {
		} finally {

			objUser = new tbl_User();
			objUser = DatabaseSupport.getTbl_User(SplashScreenActivity.this);
			if (objUser != null) {
				Const.AUTHENKEY = objUser.getToken();
				DashBoardScreen.callIntent(SplashScreenActivity.this);
			} else {
				Login.callIntent(SplashScreenActivity.this);
				finish();
			}

		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			active = false;
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		System.exit(0);
	}

}
